package my.http;


import com.app.onebuy.basis.Constants;
import com.app.onebuy.bean.LoginBean;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;

import my.LogUtil;
import my.MD5;
import okhttp3.MultipartBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;


public class MyHttpUtils {
    public final static String tag = "HTTP";
    public final static boolean DEBUG = Constants.DEBUG;

    private static final String BASE_URL = "";

    private static OkHttpClient okHttpClient;
    private static final int TIME_OUT = 30 * 1000;


    public static OkHttpClient getAsyncHttpClient() {

        if(okHttpClient == null){
            okHttpClient = new  OkHttpClient.Builder().addInterceptor(new MyInterceptor()).build();
        }
        return okHttpClient;
    }


    public static void get(String url, MyParams myParams,
                           MyHttpListener mListener, int what, Class<?> class1) {

        Request.Builder buildRquest = new Request.Builder()
                .url(getAbsoluteUrl(url))
                .put(myParams.getRequestBody())
                .get();
        AddHeaderToBuilder(buildRquest,myParams,true);
        Request request = buildRquest.build();
        getAsyncHttpClient().newCall(request).enqueue(new MyHttpResposeHandler(mListener, what, class1));
    }


    public static void post(String url, MyParams myParams,
                            MyHttpListener mListener, int what, Class<?> class1) {
        Request.Builder buildRquest = new Request.Builder()
                .url(getAbsoluteUrl(url))
                .post(myParams.getRequestBody());
        AddHeaderToBuilder(buildRquest,myParams,false);
        Request request = buildRquest.build();
        getAsyncHttpClient().newCall(request).enqueue(new MyHttpResposeHandler(mListener, what, class1));
    }

//    public static void post(String url, MyParams myParams,
//                            Callback mListener) {
//        Request.Builder buildRquest = new Request.Builder()
//                .url(getAbsoluteUrl(url))
//                .post(myParams.getRequestBody());
//        AddHeaderToBuilder(buildRquest,myParams,false);
//        Request request = buildRquest.build();
//        getAsyncHttpClient().newCall(request).enqueue(mListener);
//    }

    public static void postFile(String url, MyParams myParams,
                                MyHttpListener mListener, Class<?> class1) {
        Request.Builder buildRquest = new Request.Builder()
                .url(getAbsoluteUrl(url))
                .post(myParams.getRequestBodyWithFile());
        AddHeaderToBuilder(buildRquest,myParams,false);
        Request request = buildRquest.build();
            getAsyncHttpClient().newCall(request).enqueue(new MyHttpResposeHandler(mListener, 0, class1));

    }

    public static void postFileExcute(String url, MyParams myParams,
                                      MyHttpListener mListener, Class<?> class1) {
        MultipartBody.Builder builder =  new MultipartBody.Builder();
        Request.Builder buildRquest = new Request.Builder()
                .url(getAbsoluteUrl(url))
                .post(myParams.getRequestBodyWithFile());
        AddHeaderToBuilder(buildRquest,myParams,false);
        Request request = buildRquest.build();
        try {
            Response response = getAsyncHttpClient().newCall(request).execute();
            MyHttpResposeHandlerForSync myhandler = new MyHttpResposeHandlerForSync(mListener,0,class1);
            myhandler.onResponse(null,response);
        } catch (IOException e) {
            e.printStackTrace();
            mListener.onFailure(0,null);
        }

    }

    private static String getAbsoluteUrl(String relativeUrl) {
        LogUtil.i(tag,"getAbsoluteUrl--->"+relativeUrl);
        if(relativeUrl.startsWith("http")){
            return relativeUrl;
        }else{

//			return Constants.getServerIp() + relativeUrl;
            return relativeUrl;
        }
    }

//    private static void AddHeaderToBuilder(Request.Builder build, MySocketParams myParams, boolean isGet){
//        build.addHeader("Content-Type", "application/json");
//        if(LoginBean.isLogin()){
//            build.addHeader("Authorization", LoginBean.getInstance().getToken_type()+" "+LoginBean.getUserToken());
//        }
//    }

//    private static final String HEADER_HTTP_SourceID = "156508686904";//appid测试用
//    private static final String HEADER_HTTP_AuthKey = "hpWexNVW1LEKGrBv";
//


    private static final String HEADER_PARAMS1 = "XMGOV-API-SourceID";
    private static final String HEADER_PARAMS2 = "XMGOV-API-AuthTime";
    private static final String HEADER_PARAMS3 = "XMGOV-API-AuthKey";
    private static final String HEADER_HTTP_SourceID = "1";
    private static final String HEADER_HTTP_AuthKey = "asdf";

    private static void AddHeaderToBuilder(Request.Builder build, MyParams myParams, boolean isGet){
//        build.addHeader("Content-Type", "application/json");
        if(isGet){
            build.addHeader("Content-Type", "application/x-www-form-urlencoded");
        }

//        build.addHeader("APP-TOKEN", LoginBean.getUserToken());
//        long time = new Date().getTime();
//        build.addHeader("APP-ID", HEADER_HTTP_SourceID);
//        build.addHeader(
//                "APP-SIGN",
//                MD5.getMD5ofStr(
//                        HEADER_HTTP_AuthKey + time + HEADER_HTTP_SourceID)
//                        .substring(0, 32));
//        build.addHeader("APP-AUTHTIME", time + "");
//        build.addHeader("APP-TYPE",  "Android");
//        build.addHeader("APP-DEVICE",
//                SystemParamsUtils.getPhoneModel() + "");
//        build.addHeader("APP-OS",
//                Build.VERSION.RELEASE + "");
//        build.addHeader("APP-LANG", ChangeLanguageHelper.getLanguageHeader());

        //		LogUtil.i("APP-LANG", ChangeLanguageHelper.getLanguageHeader());
//		LogUtil.i("APP-DEVICE",
//				SystemParamsUtils.getPhoneModel() + "");
//		LogUtil.i("APP-OS",
//				Build.VERSION.RELEASE + "");
        LogUtil.i("APP-TOKEN", LoginBean.getUserToken());
    }
//
    private static String getSecretStr(ArrayList<String> list, long timestamp) {
        String result = "";
        Collections.sort(list);
        StringBuilder builder = new StringBuilder();
        builder.append(timestamp+".");
        for(String data:list){
//            LogUtil.i(""+data);
            builder.append("&").append(data);
        }
        builder.append("&").append(".").append(timestamp);

//        builder.append("appkey="+HEADER_HTTP_AuthKey);
//        LogUtil.i("字符串"+builder.toString());
//        result = MD5.getMD5ofStrLowercase(builder.toString()).substring(0,32);
        result = MD5.getMD5ofStr(builder.toString());

//        if(result.length()> 48){
//            result = result.substring(0,48).toUpperCase();
//        }
//            LogUtil.i("签名值"+result);
        return result;
    }

}


